{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2023 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# marathon2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/marathon2.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/marathon2.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Marathon puzzle in Google CP Solver.\n",
    "\n",
    "  From Xpress example\n",
    "  http://www.dashoptimization.com/home/cgi-bin/example.pl?id=mosel_puzzle_5_3\n",
    "  '''\n",
    "  Dominique, Ignace, Naren, Olivier, Philippe, and Pascal\n",
    "  have arrived as the first six at the Paris marathon.\n",
    "  Reconstruct their arrival order from the following\n",
    "  information:\n",
    "  a) Olivier has not arrived last\n",
    "  b) Dominique, Pascal and Ignace have arrived before Naren\n",
    "     and Olivier\n",
    "  c) Dominique who was third last year has improved this year.\n",
    "  d) Philippe is among the first four.\n",
    "  e) Ignace has arrived neither in second nor third position.\n",
    "  f) Pascal has beaten Naren by three positions.\n",
    "  g) Neither Ignace nor Dominique are on the fourth position.\n",
    "\n",
    "     (c) 2002 Dash Associates\n",
    "    author: S. Heipcke, Mar. 2002\n",
    "  '''\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/marathon2.mzn\n",
    "  * SICStus Prolog: http://www.hakank.org/sicstus/marathon2.pl\n",
    "  * ECLiPSe: http://hakank.org/eclipse/marathon2.ecl\n",
    "  * Gecode: http://hakank.org/gecode/marathon2.cpp\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Marathon')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 6\n",
    "\n",
    "  runners_str = [\n",
    "      'Dominique', 'Ignace', 'Naren', 'Olivier', 'Philippe', 'Pascal'\n",
    "  ]\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  runners = [solver.IntVar(1, n, 'runners[%i]' % i) for i in range(n)]\n",
    "  Dominique, Ignace, Naren, Olivier, Philippe, Pascal = runners\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.AllDifferent(runners))\n",
    "\n",
    "  # a: Olivier not last\n",
    "  solver.Add(Olivier != n)\n",
    "\n",
    "  # b: Dominique, Pascal and Ignace before Naren and Olivier\n",
    "  solver.Add(Dominique < Naren)\n",
    "  solver.Add(Dominique < Olivier)\n",
    "  solver.Add(Pascal < Naren)\n",
    "  solver.Add(Pascal < Olivier)\n",
    "  solver.Add(Ignace < Naren)\n",
    "  solver.Add(Ignace < Olivier)\n",
    "\n",
    "  # c: Dominique better than third\n",
    "  solver.Add(Dominique < 3)\n",
    "\n",
    "  # d: Philippe is among the first four\n",
    "  solver.Add(Philippe <= 4)\n",
    "\n",
    "  # e: Ignace neither second nor third\n",
    "  solver.Add(Ignace != 2)\n",
    "  solver.Add(Ignace != 3)\n",
    "\n",
    "  # f: Pascal three places earlier than Naren\n",
    "  solver.Add(Pascal + 3 == Naren)\n",
    "\n",
    "  # g: Neither Ignace nor Dominique on fourth position\n",
    "  solver.Add(Ignace != 4)\n",
    "  solver.Add(Dominique != 4)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  db = solver.Phase(runners, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
    "                    solver.ASSIGN_CENTER_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    runners_val = [runners[i].Value() for i in range(n)]\n",
    "    print('runners:', runners_val)\n",
    "    print('Places:')\n",
    "    for i in range(1, n + 1):\n",
    "      for j in range(n):\n",
    "        if runners_val[j] == i:\n",
    "          print('%i: %s' % (i, runners_str[j]))\n",
    "    print()\n",
    "\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime(), 'ms')\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
